package top.someapp.applib.kits;

import android.content.Context;
import android.content.res.XmlResourceParser;
import android.graphics.drawable.Drawable;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.RawRes;
import androidx.annotation.XmlRes;
import java.io.IOException;
import java.io.InputStream;

/**
 * 和Context相关的便捷方法
 *
 * @author zwz
 * Created on 2020-03-30
 */
public class Ctx {

    private Ctx() {
        // no need instance.
    }

    public static void toastShort(@NonNull Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

    public static void toastLong(@NonNull Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_LONG).show();
    }

    public static InputStream openAsset(@NonNull Context context, @NonNull String name)
        throws IOException {
        return context.getAssets().open(name);
    }

    public static InputStream openResource(@NonNull Context context, @RawRes int resId) {
        return context.getResources().openRawResource(resId);
    }

    public static XmlResourceParser openXmlResource(@NonNull Context context, @XmlRes int resId) {
        return context.getResources().getXml(resId);
    }

    public static Drawable assetsDrawable(@NonNull Context context, @NonNull String fileName) {
        Drawable drawable = null;
        try (InputStream open = context.getAssets().open(fileName)) {
            drawable = Drawable.createFromStream(open, null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return drawable;
    }
}
